package com.atguigu.controller;


import com.atguigu.constant.MessageConstant;
import com.atguigu.entity.Result;
import com.atguigu.utils.SMSUtils;
import com.atguigu.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;


@RestController
@RequestMapping("code")
public class CodeController {
    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("send")
    public Result sendCode(String telephone){
        Jedis jedis = jedisPool.getResource();
        try {
            String code = ValidateCodeUtils.generateValidateCode4String(4);
            SMSUtils.sendShortMessage(telephone,code);
            jedis.setex(telephone,5*60,code);
            return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }finally {
            jedis.close();
        }
    }
}
